package com.ostrichmyself.txtReader;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class ViewFile extends Activity {

    private static final String gb2312 = "GB2312";
    private static final String utf8 = "UTF-8";
    private static final String defaultCode = gb2312;
    private String filenameString;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.filebrowser);
        try {
            Bundle bunde = this.getIntent().getExtras();
            filenameString = bunde.getString("fileName");
            refreshGUI(defaultCode);
        } catch (Exception e) {
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = this.getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        TextView tv=(TextView)findViewById(R.id.view_contents);
        switch (item.getItemId()) {
            case R.id.gb2312:
                refreshGUI(defaultCode);
                break;
            case R.id.utf8:
                refreshGUI(utf8);
                break;
            case R.id.about:
                doAbout();
                break;
            case R.id.color:
                setColor();
                break;
            case R.id.plus:
                tv.setTextSize(tv.getTextSize()+2);
                break;
            case R.id.down:
                tv.setTextSize(tv.getTextSize()-2);
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void setColor() {
        AlertDialog.Builder builder = new AlertDialog.Builder(ViewFile.this);
        builder.setItems(getResources().getStringArray(R.array.colormenu), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                TextView tv = (TextView) findViewById(R.id.view_contents);
                switch (arg1){
                    case 0:
                        tv.setBackgroundColor(getResources().getColor(R.color.white));
                        tv.setTextColor(getResources().getColor(R.color.black));
                        break;
                    case 1:
                        tv.setBackgroundColor(getResources().getColor(R.color.black));
                        tv.setTextColor(getResources().getColor(R.color.white));
                        break;
                    case 2:
                        tv.setBackgroundColor(getResources().getColor(R.color.blue));
                        tv.setTextColor(getResources().getColor(R.color.black));
                        break;
                    case 3:
                        tv.setBackgroundColor(getResources().getColor(R.color.red));
                        tv.setTextColor(getResources().getColor(R.color.black));
                        break;
                    case 4:
                        tv.setBackgroundColor(getResources().getColor(R.color.rice));
                        tv.setTextColor(getResources().getColor(R.color.black));
                        break;
                    case 5:
                        tv.setBackgroundResource(R.drawable.backa);
                        tv.setTextColor(getResources().getColor(R.color.black));
                    default:
                        break;
                }
            }
        }).show();
    }

    private void doAbout() {
        String s=getString(R.string.aboutInfo)+"\n"+getString(R.string.hint);
        Dialog dialog = new AlertDialog.Builder(ViewFile.this).setTitle(

                R.string.aboutTitle).setMessage(s)
                .setPositiveButton(R.string.aboutOK,
                        new DialogInterface.OnClickListener() {
                            public void onClick(
                                    DialogInterface dialoginterface, int i) {
                                //
                            }
                        }).create();
        dialog.show();
    }

    private void refreshGUI(String code) {
        TextView tv = (TextView) findViewById(R.id.view_contents);
        String fileContent = getStringFromFile(code);
        tv.setText(fileContent);
    }

    public String getStringFromFile(String code) {
        try {
            StringBuffer sBuffer = new StringBuffer();
            FileInputStream fInputStream = new FileInputStream(filenameString);
            InputStreamReader inputStreamReader = new InputStreamReader(fInputStream, code);
            BufferedReader in = new BufferedReader(inputStreamReader);
            if (!new File(filenameString).exists()) {
                return null;
            }
            while (in.ready()) {
                sBuffer.append(in.readLine() + "\n");
            }
            in.close();
            return sBuffer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



    public byte[] readFile(String fileName) throws Exception {
        byte[] result = null;
        FileInputStream fis = null;
        try {
            File file = new File(fileName);
            fis = new FileInputStream(file);
            result = new byte[fis.available()];
            fis.read(result);
        } catch (Exception e) {
        } finally {
            fis.close();
        }
        return result;
    }

}
